package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * 
 * @author axi
 *
 */
public class N21Analyse implements AnalyseNews {

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://[a-z]*.n21.cc/.*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/content_[0-9]+.shtml",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta video = new NewsMeta();
		String title = "";
		String desc = "";
		String author = "";
		Long date = null;
		Integer commentNum=0;
		Integer clickNum=0;
		
		/**
		 * 标题
		 */
		title = doc.select("h1#title").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("h1").text();
		}
		desc = HtmlCleaner.getContentHtml(url, doc.select("div.contwenzi"));
		if(StringUtils.isBlank(desc)){
			desc = HtmlCleaner.getContentHtml(url, doc.select("div.article"));
		}
		date = DateUtils.matchDate(doc.select("#time").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.date").text());
		}
		if("".equals(title)){
		}
		
		author = doc.select("span.src").text();
		author = StringUtils.substringAfter(author, "来源:");
		
		video.setUrl(url);
		video.setTitle(title);
		video.setContent(desc);
		video.setAuthor(author);
		video.setDate(date);
		video.setClickNum(clickNum);
		video.setCommentNum(commentNum);
		video.setType(2);
		return video;
	}

	/**
	 * @param args
	 */
	

	
}
